﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Configuration;
using Controller.Enum;
using Model;
using Newtonsoft.Json.Linq;

namespace Controller.Helper
{
    public class Approver
    {
        public static string GetSaleClientByApproval(string approvaerUsername)
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var sales = from approvalMapping in dataContext.tblApprovalSaleMappings
                            where approvalMapping.ApprovalUserName.Equals(approvaerUsername)
                            select approvalMapping.SaleUsername;

                JObject k = JObject.FromObject(new
                {
                    AllSale = sales
                });
                return k.ToString();
            }

        }

        public static List<string> GetApprovalMailBySale(string saleUserName)
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var approvers = from approvalMapping in dataContext.tblApprovalSaleMappings
                                join userDetail in dataContext.tblUsers on approvalMapping.ApprovalUserName equals userDetail.UserName
                                where approvalMapping.SaleUsername.Equals(saleUserName) && userDetail.IsDisable != true
                                select userDetail.Email;

                List<string> emails = approvers.ToList<string>();

                return emails;
            }

        }
        public static List<tblUser> GetApprovalBySale(string saleUserName)
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var approvers = from approvalMapping in dataContext.tblApprovalSaleMappings
                                join userDetail in dataContext.tblUsers on approvalMapping.ApprovalUserName equals userDetail.UserName
                                where approvalMapping.SaleUsername.Equals(saleUserName) && userDetail.IsDisable != true
                                select userDetail;


                return approvers.ToList();
            }

        }

        
        public static string GetAllApprover()
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                string ApproverRoleName = WebConfigurationManager.AppSettings["ApproverRoleName"] ?? "Approver";
                var sales = from approvals in dataContext.tblUsers
                            where approvals.RoleName.Equals(ApproverRoleName) && approvals.IsDisable != true
                            select approvals;

                JObject k = JObject.FromObject(new
                {
                    AllApprover = sales
                });
                return k.ToString();
            }
        }

        public static string GetAllApproveLevel()
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var aps = from ap in dataContext.tblApprovals
                          select ap;
                JObject k = JObject.FromObject(new
                {
                    AllApprover = aps
                });
                return k.ToString();
            }
        }
        public static List<tblApproval> GetAllApproveLevelObject()
        {
            using (var dataContext = new DCSalePriceDataContext())
            {
                var aps = from ap in dataContext.tblApprovals
                          select ap;
                
                return aps.ToList();
            }
        }
        public static bool SaveAllApproveLevel(List<tblApproval> approvals )
        {
            using (var dataContext = new DCSalePriceDataContext())
            {
                try
                {
                    foreach (var tblApproval in approvals)
                    {
                        var oldApproval = dataContext.tblApprovals.Single(a => a.approveName == tblApproval.approveName);
                        oldApproval.approveDisplay = tblApproval.approveDisplay;
                        oldApproval.warningColor = tblApproval.warningColor.StartsWith("#") ? tblApproval.warningColor : "#" + tblApproval.warningColor;

                    }
                    dataContext.SubmitChanges();

                    return true;
                }
                catch (Exception)
                {

                    return false;
                }
                
            }
        }
        public static  string GetAllApproverSaleMapping()
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var sales = from approvalsales in dataContext.tblApprovalSaleMappings
                            join userDetail in dataContext.tblUsers on approvalsales.ApprovalUserName equals userDetail.UserName
                            where userDetail.IsDisable != true
                            select new
                                       {
                                           approvalsales.ApprovalUserName,
                                           approvalsales.SaleUsername,
                                           approverName = userDetail.FirstName + " " + userDetail.Lastname
                                       };

                var data = from s in sales
                           join userdetail in dataContext.tblUsers on s.SaleUsername equals userdetail.UserName
                           select new
                                      {
                                          s.SaleUsername,
                                          s.ApprovalUserName,
                                          s.approverName,
                                          saleName = userdetail.FirstName + " " + userdetail.Lastname
                                      };
                JObject k = JObject.FromObject(new
                {
                    AllApproverSale = data
                });
                return k.ToString();
            }
        }

        public static int ApproverAction(List<tblApprovalSaleMapping> approvalSaleMappings, EnumAction action)
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                if (approvalSaleMappings != null && approvalSaleMappings.Count > 0)
                {
                    switch (action)
                    {
                        case EnumAction.Update:
                            try
                            {
                                foreach (tblApprovalSaleMapping newapprovalSaleMapping in approvalSaleMappings)
                                {
                                    tblApprovalSaleMapping oldapprovalMapping = dataContext.tblApprovalSaleMappings.Single(approvalMapping => approvalMapping.ApprovalUserName == newapprovalSaleMapping.ApprovalUserName && approvalMapping.SaleUsername == newapprovalSaleMapping.SaleUsername);
                                    oldapprovalMapping.isDisable = newapprovalSaleMapping.isDisable;
                                    oldapprovalMapping.Comment = newapprovalSaleMapping.Comment;
                                }
                                dataContext.SubmitChanges();
                                return approvalSaleMappings.Count;
                            }
                            catch (Exception ex)
                            {

                                return -1;
                            }
                        case EnumAction.Insert:
                            try
                            {

                                var existingFoos = from exits in dataContext.tblApprovalSaleMappings
                                                   select exits;
                                var approvalToInsert = approvalSaleMappings.Where(newFoo => !existingFoos.Any(existingFoo => newFoo.ApprovalUserName == existingFoo.ApprovalUserName && newFoo.SaleUsername == existingFoo.SaleUsername));
                                int update = approvalToInsert.Count();

                                if (update > 0)
                                {
                                    dataContext.tblApprovalSaleMappings.InsertAllOnSubmit(approvalToInsert);
                                    dataContext.SubmitChanges();
                                    return update;
                                }

                                else
                                {
                                    dataContext.ExecuteCommand("delete from tblApprovalSaleMapping");
                                    dataContext.tblApprovalSaleMappings.InsertAllOnSubmit(approvalSaleMappings);
                                    dataContext.SubmitChanges();
                                    return approvalSaleMappings.Count;
                                }
                                return update;

                            }
                            catch (Exception ex)
                            {
                                return -1;
                            }
                        case EnumAction.Delete:
                            try
                            {
                                var approvaMapping = from newapprovalSaleMapping in dataContext.tblApprovalSaleMappings
                                              where approvalSaleMappings.Contains(newapprovalSaleMapping)
                                              select newapprovalSaleMapping;
                                //dataContext.tblUsers.DeleteByPK<tblUser>(users.AsQueryable());
                                int deletedApprovalMapping = dataContext.tblApprovalSaleMappings.DeleteBatch<tblApprovalSaleMapping>(approvaMapping);
                                //dataContext.tblUsers.UpdateBatch<tblUser>(users1, p => new tblUser{ Email = p.DateCreated, 
                                //           UserName = "Chris Cavanagh" } );
                                //dataContext.tblUsers.DeleteAllOnSubmit(users);
                                //dataContext.SubmitChanges();
                                return deletedApprovalMapping;
                            }
                            catch (Exception)
                            {

                                return -1;
                            }
                    }
                }
                else
                {
                    return 0;
                }
            }
            return 0;
        }
    }
}
